#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef __linux__
#include <sys/mman.h>
#include <unistd.h>
#endif

int mark_writable(char *s);

int main()
{
    char *s = "hello world!";
    if (mark_writable(s)) {
        return -1;
    }
    s[0] = 'H';
    printf("%s\n", s);
    return 0;
}

int mark_writable(char *s)
{
#ifdef _WIN32
    DWORD dwOldProtect;
    SYSTEM_INFO si;
    GetSystemInfo(&si);
    if (!VirtualProtect(s, si.dwPageSize, PAGE_EXECUTE_READWRITE, &dwOldProtect)) {
        perror("VirtualProtect");
        return -1;
    }
    return 0;
#endif
#ifdef __linux__
    long pagesize, pagestart;
    pagesize = sysconf(_SC_PAGESIZE);
    pagestart = (long)s & ~(pagesize-1);
    if (mprotect((void *)pagestart, pagesize, PROT_READ | PROT_WRITE | PROT_EXEC)) {
        perror("mprotect");
        return -1;
    }
    return 0;
#endif
    return -1;
}

